//    Copyright (C) Kherty.  All rights reserved.


using OpenLS.Spreadsheet.Extensibility;

namespace OpenLS.Spreadsheet.StandardAddIn
{
    [AddInFunctions]
    [AllowNullToBooleanConversionAttribute, AllowDoubleToBooleanConversion]
    public static class LogicalFunctions
    {
        public static bool True()
        {
            return true;
        }

        public static bool False()
        {
            return false;
        }

        public static object If([AllowDoubleToBooleanConversion] bool condition, [Optional(false)]object ifTrue, [Optional(false)]object ifFalse)
        {
            if (condition)
                return ifTrue;
            return ifFalse;
        }
        public static object IfError(object value, object value_if_error)
        {
            if (value is Error)
                return value_if_error;
            return value;
        }

        public static object And([AllowDoubleToBooleanConversion] params bool[] logical)
        {
            if (logical.Length == 0)
                return Errors.ValueError;
            foreach (bool b in logical)
                if (!b)
                    return false;
            return true;
        }

        public static object Or([AllowDoubleToBooleanConversion] params bool[] logical)
        {
            if (logical.Length == 0)
                return Errors.ValueError;
            foreach (bool b in logical)
                if (b)
                    return true;
            return false;
        }

        public static bool Not(bool logical)
        {
            return !logical;
        }
    }
}